<?php


namespace app\admin\controller;


use think\Controller;

class AlterPhoto extends Controller
{

    //轮播图列表
    public function lst(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        if (request()->isPost()){
           $data=input('post.');
            foreach ($data as $k=>$value) {
                foreach ($value as $k => $v) {
                    db('alterphoto')->where('id', $k)->update(['sort' => $v]);
                }
            }
            $this->success('排序成功!','lst');
        }
        $alterdata=db('alterphoto')->order('sort','esc')->select();
        $this->assign('alter',$alterdata);
        $this->assign('numleft',4);
        return view('list');
    }


    //添加轮播图
    public function add(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        if(request()->isPost()){
            $data=input('post.');
            if(stripos($data['url_link'],'http://')===false){
                $data['url_link']='http://'.$data['url_link'];
            }
            db('alterphoto')->insert(['img_src'=>$data['img_src'],'url_link'=>$data['url_link']]);
            $this->success('添加轮播图成功','add');
            
        }
        $this->assign('numleft',4);
            return view();
    }

    //删除轮播图
    public function del($id){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        db('alterphoto')->delete($id);
        
        $this->success('成功删除轮播图','/admin/alter_photo/lst');
    }


    public function uploads()//上传模块
    {
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move('../public/static/admin/uploads');
        if(!$info){
            $this->error($file->getError());
        }else{
            $file_a= $info->getFilename();
            echo '{"code":0,"msg":"成功上传","url":"'.$file_a.'","date":"'.date("Ymd").'"}';
        }
        unset($info);
    }
}